package fr.ordre66.vlcs.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import fr.ordre66.vlcs.dto.User;
import fr.ordre66.vlcs.services.IUserManager;


@Controller
@RequestMapping("/register")
public class Users {

	@Autowired IUserManager userManager;
	
    @RequestMapping(method = RequestMethod.GET)
    public String accessRegister(Model model) {
        
    	model.addAttribute("left", "register.jsp");
    	model.addAttribute("right", "menu.jsp");
    	return "template";
    }
	
    @RequestMapping(method = RequestMethod.POST)
    public String register(String login, String firstname, String familyname, String email, String password, Model model) {
        
    	System.out.println("Register a new user with login: " + login);
    	
		User user = new User();
		user.setLogin(login);
		user.setFamilyname(familyname);
		user.setFirstname(firstname);
		user.setEmail(email);
		user.setPassword(password);
		userManager.create(user);
    	
//    	model.addAttribute("left", "home.jsp");
//    	model.addAttribute("right", "menu.jsp");
//     	return "template";
		return "redirect:/home";
    }
}
